<template>
	<el-table :data="categoryData" row-key="id">
		<el-table-column prop="name" label="分类名称">
			
		</el-table-column>
		<el-table-column label="操作">
			<template #default="scope">
				<el-button type="primary" @click="add(scope.row.id)">
					添加
				</el-button>
				<el-button type="warning" @click="update(scope.row)">
					修改
				</el-button>
				<el-button color="red" @click="del(scope.row.id)">
					删除
				</el-button>
			</template>
		</el-table-column>
	</el-table>
	<el-dialog
	    v-model="dialogVisible"
	    title="添加分类"
	    width="30%"
	  >
	  <el-input placeholder="请输入分类名" v-model="name"></el-input>
	    <template #footer>
	      <span class="dialog-footer">
	        <el-button @click="dialogVisible = false">Cancel</el-button>
	        <el-button type="primary" @click="addSubmit"
	          >Confirm</el-button
	        >
	      </span>
	    </template>
	  </el-dialog>
	  <el-dialog
	      v-model="dialogVisible_update"
	      title="修改分类"
	      width="30%"
	    >
	    <el-input placeholder="请输入分类名" v-model="name"></el-input>
		<!-- 选择父级： -->
		 <!-- <el-tree-select
		    v-model="pid"
		    :data="categoryData_tree"
		    check-strictly
		    :render-after-expand="false"
			 :props="defaultProps"
			 node-key="id"
		  /> -->
	      <template #footer>
	        <span class="dialog-footer">
	          <el-button @click="dialogVisible_update = false">Cancel</el-button>
	          <el-button type="primary" @click="updateSubmit"
	            >Confirm</el-button
	          >
	        </span>
	      </template>
	    </el-dialog>
		<el-dialog
		    v-model="dialogVisible_del"
		    title="删除分类"
		    width="30%"
		  >
		  你确定要删除id为{{id}}这个分类吗？
		    <template #footer>
		      <span class="dialog-footer">
		        <el-button @click="dialogVisible_del = false">Cancel</el-button>
		        <el-button type="primary" @click="delSubmit"
		          >Confirm</el-button
		        >
		      </span>
		    </template>
		  </el-dialog>
</template>

<script setup>
	import {ref} from 'vue'
	import http from './request.js'
	const categoryData = ref([])
	const categoryData_tree = ref([])
	const name= ref('')
	const pid = ref(0)
	const dialogVisible = ref(false)
	const dialogVisible_update = ref(false)
	const dialogVisible_del = ref(false)
	const id = ref(0)
	const defaultProps = {
	  children: 'children',
	  label: 'name',
	}
	function query(){
		http.get('/category/query').then(res=>{
			categoryData.value = res.data
		})
	}
	query()
	function add(e){
		console.log(e)
		id.value = e
		name.value = ''
		dialogVisible.value = true
		
	}
	function addSubmit(){
		http.post('/category/save',{pid:id.value,name:name.value}).then(res=>{
			dialogVisible.value = false
			query()
		})
	}
	function update(e){
		console.log(e)
		id.value = e.id
		name.value = e.name
		dialogVisible_update.value = true
		// categoryData_tree.value = [{id:0,name:'根目录',children:categoryData.value}]
	}
	function updateSubmit(){
		http.post('/category/update',{id:id.value,name:name.value}).then(res=>{
			dialogVisible_update.value = false
			query()
		})
	}
	function del(e){
		console.log(e)
		id.value = e
		dialogVisible_del.value = true
	}
	function delSubmit(){
		http.get('/category/del?id='+id.value).then(res=>{
			query()
			dialogVisible_del.value = false
		})
		
	}
</script>

<style>
</style>